﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGBTech.Core.Models.ChungTu;
using TGB.Core.Infrastructure;

namespace TGBTech.Web.Controllers
{
    public class ChungTuController : Controller
    {
        private TGBTechDb db = new TGBTechDb();

        // GET: /ChungTu/
        //[Authorize(Roles="Admin")]
        public async Task<ActionResult> Index()
        {
            var chungtu = db.ChungTu.Include(c => c.KhachHang).Include(c => c.TaiKhoanCo).Include(c => c.TaiKhoanNo);
            return View(await chungtu.ToListAsync());
        }

        // GET: /ChungTu/Details/5
        public async Task<ActionResult> Details(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTu chungtu = await db.ChungTu.FindAsync(id);
            if (chungtu == null)
            {
                return HttpNotFound();
            }
            return View(chungtu);
        }

        // GET: /ChungTu/Create
        public ActionResult Create()
        {
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name");
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name");
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name");
            return View();
        }

        // POST: /ChungTu/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include="Id,NgayCT,TaiKhoanNoId,TaiKhoanCoId,LyDoNhapXuat,KhachHangId,SoHieu,SoHoaDon,NgayHoaDon")] ChungTu chungtu)
        {
            if (ModelState.IsValid)
            {
                db.ChungTu.Add(chungtu);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name", chungtu.KhachHangId);
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanCoId);
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanNoId);
            return View(chungtu);
        }

        // GET: /ChungTu/Edit/5
        public async Task<ActionResult> Edit(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTu chungtu = await db.ChungTu.FindAsync(id);
            if (chungtu == null)
            {
                return HttpNotFound();
            }
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name", chungtu.KhachHangId);
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanCoId);
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanNoId);
            return View(chungtu);
        }

        // POST: /ChungTu/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include="Id,NgayCT,TaiKhoanNoId,TaiKhoanCoId,LyDoNhapXuat,KhachHangId,SoHieu,SoHoaDon,NgayHoaDon")] ChungTu chungtu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chungtu).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.KhachHangId = new SelectList(db.KhachHang, "Id", "Name", chungtu.KhachHangId);
            ViewBag.TaiKhoanCoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanCoId);
            ViewBag.TaiKhoanNoId = new SelectList(db.TaiKhoan, "Id", "Name", chungtu.TaiKhoanNoId);
            return View(chungtu);
        }

        // GET: /ChungTu/Delete/5
        public async Task<ActionResult> Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChungTu chungtu = await db.ChungTu.FindAsync(id);
            if (chungtu == null)
            {
                return HttpNotFound();
            }
            return View(chungtu);
        }

        // POST: /ChungTu/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            ChungTu chungtu = await db.ChungTu.FindAsync(id);
            db.ChungTu.Remove(chungtu);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
